package x86;

import hex.Instruction;
import hex.Operand;
import hex.Operator;

/**
 * JMP
 * 
 * @author qj
 * 
 */
public class JMP extends Detector {
	public final static JMP INSTANCE = new JMP();

	public boolean detect() {
		// short 1110 1011 : 8-bit displacement
		// direct 1110 1001 : full displacement
		// register indirect 1111 1111 : 11 100 reg
		// memory indirect 1111 1111 : mod 100 r/m

		int b = input.getByte();
		int b2 = input.prefetchByte();
		init(b, b2);

		switch (b) {
		case 0xEB:// short?
			new Instruction(Operator.JMP.INSTANCE, input.getByte(), 2).Add();
			break;
		case 0xE9:// direct?
			new Instruction(Operator.JMP.INSTANCE, input.getLEDWORD(), 5).Add();
			break;
		case 0xFF:// reg?
			if ((b2 & 0xE0) > 0)
				new Instruction(Operator.JMP.INSTANCE,
						vm.GlobalRegister[modrm_rm], 2).Add();
			else if ((b2 & 0x20) > 0) {
				Operand t = decode_modrm();
				new Instruction(Operator.LDM.INSTANCE, t,
						new Operand.NullOperand(), vm.TempRegister[1], 0).Add();

				new Instruction(Operator.JMP.INSTANCE, vm.TempRegister[1], 2)
						.Add();
			}
			break;
		}

		return NOT_MATCHED;
	}
}
